package com.example.image.loder

import android.content.Context
import android.net.Uri
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.example.image.option.ImageOptions
import com.example.image.skill.ImageLoader
import java.io.File
import java.lang.ref.SoftReference

/**
 * Glide图片加载器
 * */

class GlideImgLoader : ImageLoader {

    override fun loadFileIntoImageView(context: Context, file: File, target: ImageView, options: ImageOptions) {
        loadImageWithGlide(context,file,target,options)
    }

    override fun loadResIntoImageView(context: Context, resId: Int, target: ImageView, options: ImageOptions) {
        loadImageWithGlide(context,resId,target,options)
    }

    override fun loadUrlIntoImageView(context: Context, uri: Uri, target: ImageView, options: ImageOptions) {
        loadImageWithGlide(context,uri,target,options)
    }

    override fun loadNetIntoImageView(context: Context, url: String, target: ImageView, options: ImageOptions) {
        loadImageWithGlide(context,url,target,options)
    }

    override fun loadBytesIntoImageView(context: Context, bytes: ByteArray, target: ImageView, options: ImageOptions) {
        loadImageWithGlide(context,bytes,target,options)
    }

    private fun loadImageWithGlide(context: Context,source : Any,target: ImageView, options: ImageOptions){
       //使用软引用来施行内存回收
        var softRof =SoftReference(context)
        softRof.get()?.apply {

            var requesManager =Glide.with(this)

            //是否加载静态图片 (gif转成png)
            if (options.isGif && options.isStaticImg){
                requesManager.asBitmap()
            }

           var requestBuilder= requesManager.load(source)

            //设置占位图片
            if(options.placeImage>0){
                requestBuilder.placeholder(options.placeImage)
            }
            //设置错误占位图片
            if(options.errorImage>0){
                requestBuilder.error(options.errorImage)
            }
            //是否需要跳过内存缓存
            if (options.skipMemoryCache){
                requestBuilder.skipMemoryCache(true)
            }
            //是否跳过磁盘缓存
            if (options.diskCacheStrategy!= DiskCacheStrategy.NONE){
                requestBuilder.diskCacheStrategy(options.diskCacheStrategy)
            }
            //设置图片大小
            if (options.imgSize.size == 2) {
                requestBuilder.submit(options.imgSize[0], options.imgSize[1])
            }
            //设置圆形图片
            if (options.isCircle) {
                requestBuilder.transform(CenterCrop(), CircleCrop())
            }
            //设置圆角图片
            if (options.roundRadius > 0) {
                requestBuilder.transform(CenterCrop(), RoundedCorners(options.roundRadius))
            }
            //将获取到图片设置到ImageView
            requestBuilder.into(target)




        }


    }

}